package com.thon.controller.rest;

import java.util.Date;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.collect.Maps;
import com.thon.dao.util.Page;
import com.thon.entity.project.Project;
import com.thon.entity.project.ProjectAttention;
import com.thon.service.project.ProjectAttentionService;
import com.thon.service.project.ProjectService;

/**
 * @file ProjectAttentionRestCountroller.java
 * @author Zemo
 * @mail zemochen@gmail.com
 * @data 2013年11月18日 上午8:39:13
 * @description: TODO
 */
@Controller
@RequestMapping(value = "/api/project/attention")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ProjectAttentionRestCountroller {

	@Autowired
	private ProjectService projectService;
	@Autowired
	private ProjectAttentionService projectAttentionService;

	/**
	 * @Title: payAttention
	 * @Description: 根据项目id添加关注 并更新project表中的关注数,如果关注过则取消关注
	 * @param projectAttention
	 * @return 201关注成功
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<HttpEntity> payAttention(
			@Valid @RequestBody ProjectAttention projectAttention) {

		Integer attentionId = projectAttentionService.getProjectAttention(
				projectAttention.getUserId(), projectAttention.getProject()
						.getId());
		Map<String, Object> map = Maps.newHashMap();
		String msg="";
		if (attentionId != null) {
			projectAttentionService.delete(attentionId);
			Project project = projectService.getProject(-1, projectAttention
					.getProject().getId());
			project.setAttentionCount(project.getAttentionCount() - 1);

			projectService.updateProject(project);
			msg="Disattention Success";
		} else {
			projectAttentionService.add(projectAttention);

			Project project = projectService.getProject(-1, projectAttention
					.getProject().getId());
			project.setAttentionCount(project.getAttentionCount() + 1);

			projectService.updateProject(project);
			msg="Attention Success";

		}
		map.put("msg", msg);
		return new ResponseEntity(map, HttpStatus.CREATED);
	}

	//根据项目关注id获取项目关注
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ResponseEntity<HttpEntity> getProjectAttention(
			@RequestParam(value = "id", required = true) int id, Model model) {

		ProjectAttention projectAttention = projectAttentionService
				.getProjectAttention(id);
		model.addAttribute("projectAttention", projectAttention);
		return new ResponseEntity(model, HttpStatus.OK);
	}
	
	//根据用户id获取关注列表
	@RequestMapping(value="/user/list",method=RequestMethod.GET)
	public ResponseEntity<HttpEntity> findProjectAttentionByUser(
			@RequestParam(value="uid")Integer userId,
			@RequestParam(value="p",defaultValue="1")Integer pageNo,
			@RequestParam(value="s",defaultValue="4")Integer pageSize){
			Page<ProjectAttention> page=new Page<ProjectAttention>();
			page=projectAttentionService.findProjectAttentionByUser(userId,pageNo,pageSize);
			return new ResponseEntity(page, HttpStatus.OK);
		
	}

}
